# -*- coding: latin-1 -*-
# Copyright (c) 2007-2009 Jorge Ortiz Romo <jortizromo@gmail.com>
#                         Freymam Vallejo Cuero <freturn@gmail.com>
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:

# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
# Import the module and instantiate a pygraph object

'''
Created on 6/02/2010

@author: Freymam Vallejo
@author: Jorge Ortiz
'''

import yapgvb

def pygraph_to_yagraph (filename, pygr):
    """
    Convert a PyGraph Digraph to YapGvb Digraph. 

    @type filename: string.
    @param filename: Filename for name the image.

    @type pygr: digraph
    @param pygr: PyGraph Digraph.

    @rtype: yapgvb.Digraph.
    @return: YapGvb Digraph.
    """
    
    
    # Create a new undirected graph
    yagr = yapgvb.Digraph (filename, True)
    nodes = {}
    
    # Crea los nodos
    for node in pygr.nodes():
        nodes[node] = yagr.add_node (node)
        nodes[node].label = node
        nodes[node].comment = "comment"
        nodes[node].tooltip = "tooltip"
    
    # Adicionan las relaciones
    for n1, n2 in pygr.edges():
        edge = yagr.add_edge (nodes[n1], nodes[n2])
        edge.label = '%s' % (pygr.edge_weight (n1, n2))
    
    return yagr

# ----------------------------------------------------
# Pintar el grafo a un archivo imagen en el disco duro
# ----------------------------------------------------

def draw_graph (pygr, filename):
    """
    Draw a YapGvb Digraph into a image file. 

    @type filename: string.
    @param filename: Filename for name the image.

    @type pygr: digraph
    @param pygr: PyGraph Digraph.
    """
    
    # Write to a file
    format = yapgvb.formats.jpg
    filename = '%s.%s' % (filename, format)
    
    # Generating yapgvb graph
    #print "Generating yapgvb graph"
    yagr = pygraph_to_yagraph (filename, pygr)

    # Performing layout with dot
    #print "Performing layout with dot"
    yagr.layout (yapgvb.engines.dot)

    # Rendering
    print "Rendering %s ..." % filename
    yagr.render (filename)
    
    
